package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.SuggestionBox;
import org.zsk.cs.domain.vo.IdentityAuthVo;
import org.zsk.cs.domain.vo.SuggestionBoxVo;
import org.zsk.cs.domain.vo.SuggestionBoxVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.SuggestionBoxService;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 意见箱 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/suggestionbox")
public class SuggestionBoxController {

    private final SuggestionBoxService suggestionBoxService;
    private final Assign assign;

    @Autowired
    public SuggestionBoxController(SuggestionBoxService suggestionBoxService, Assign assign) {
        this.suggestionBoxService = suggestionBoxService;
        this.assign = assign;
    }

//    @PostMapping("/a")
//    public ResultBody add(@RequestBody SuggestionBoxVo suggestionBoxVo) throws Exception {
//        SuggestionBox suggestionBox=new SuggestionBox();
//        assign.assign(suggestionBoxVo,suggestionBox);
//        return ResultBody.success(suggestionBoxService.save(suggestionBox));
//    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody SuggestionBoxVo suggestionBoxVo) throws Exception {
        SuggestionBox suggestionBox=new SuggestionBox();
        assign.assign(suggestionBoxVo,suggestionBox);
        return ResultBody.success(suggestionBoxService.updateById(suggestionBox));
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        return ResultBody.success(suggestionBoxService.removeByIds(ids));
    }

    @GetMapping("/g")
    public ResultBody page(SuggestionBoxVo suggestionBoxVo) throws Exception {
        Page<SuggestionBox> page=new Page<>();
        assign.assign(suggestionBoxVo,page,"$current,$size");
        return ResultBody.success(suggestionBoxService.page(page));
    }

    @PostMapping("/addbyadmin")
    public ResultBody addByAdmin(@RequestBody SuggestionBoxVo suggestionBoxVo) throws Exception {
        SuggestionBox suggestionBox=new SuggestionBox();
        assign.assign(suggestionBoxVo,suggestionBox,"$content,$residentId");
        return ResultBody.success(suggestionBoxService.addByAdmin(suggestionBox));
    }

    @GetMapping("/checkbydistricthead")
    public ResultBody checkByDistrictHead(IdentityAuthVo identityAuthVo) throws Exception {
        Page<SuggestionBox> page=new Page<>();
        assign.assign(identityAuthVo,page,"$current,$size");
        return ResultBody.success(suggestionBoxService.checkByDistrictHead(identityAuthVo.getId(),page));
    }

    @PutMapping("/adoptbydistricthead")
    public ResultBody adoptByDistrictHead(@RequestBody SuggestionBoxVo suggestionBoxVo) throws Exception {
        SuggestionBox suggestionBox=new SuggestionBox();
        assign.assign(suggestionBoxVo,suggestionBox,"$id,$status");
        return ResultBody.success(suggestionBoxService.updateById(suggestionBox));
    }

    @GetMapping("/getWithUserNameByPage")
    public ResultBody getWithUserNameByPage(SuggestionBoxVo suggestionBoxVo) throws Exception {
        return ResultBody.success(suggestionBoxService.getWithUserNameByPage(suggestionBoxVo));
    }

    @GetMapping("/getWithUserNameByPageForResident")
    public ResultBody getWithUserNameByPageForResident(SuggestionBoxVo suggestionBoxVo) throws Exception {
        return ResultBody.success(suggestionBoxService.getWithUserNameByPageForResident(suggestionBoxVo));
    }

    @PostMapping("/addbyresident")
    public ResultBody addByResident(@RequestBody SuggestionBoxVo suggestionBoxVo) throws Exception {
        SuggestionBox suggestionBox=new SuggestionBox();
        assign.assign(suggestionBoxVo,suggestionBox,"$content");
        return ResultBody.success(suggestionBoxService.addByResident(suggestionBox));
    }

    @GetMapping("/getWithUserNameByPageForDistrictHead")
    public ResultBody getWithUserNameByPageForDistrictHead(SuggestionBoxVo suggestionBoxVo) throws Exception {
        return ResultBody.success(suggestionBoxService.getWithUserNameByPageForDistrictHead(suggestionBoxVo));
    }
}
